﻿using System;
using System.Collections.Generic;
using Color = System.Drawing.Color;
using SwinGame;
using System.Diagnostics;

namespace Gundam_2._0_Alpha.src
{
    public class Touhou2 : CharacterAction
    {
        private const int circleCount = 8,
                          maxRange = 800,
                          triggerRange = 350;
        private const float pathRadius = 100f,
                            degreesInc = 0.79f;

        private List<Circles> circle = new List<Circles>();
        private Timer timer = Core.CreateTimer();
        private int setCount,direction = 1, currentWave = 0, minDamage;
        private bool isPlayer;

        public Touhou2(Character c, bool persistant, int row, int colEnd, int count, int lockoutLevel, int minDamage,
            int centerX, int centerY, int setCount, bool isPlayer)
            : base(c, persistant, row, colEnd, count, lockoutLevel)
        {
            this.isPlayer = isPlayer;
            this.minDamage = minDamage;
            this.setCount = setCount;
            for (int i = 0; i < setCount; i++)
                CreateCircles(centerX,centerY);
        }

        public override void DoAction(GameController gc)
        {
            Debug.WriteLine("pre active");
            if (!IsActive)
                IsActive = true;
            Debug.WriteLine("post active");

            if (currentWave == 0 && !circle[currentWave].IsActive)
            {
                Debug.WriteLine("in currentWave 0 if");
                for (int i = currentWave * circleCount; i < circleCount + (circleCount * currentWave); i++)
                {
                    circle[i].IsActive = true;
                    gc.GameStages.Peek().ToAdd.Add(circle[i]);
                }
                Debug.WriteLine("ending currentWave 0 if");
            }
            else if (circle[circleCount + (circleCount * currentWave) - 1].PathRadius > triggerRange && currentWave <= setCount - 2)
            {
                Debug.WriteLine("in circle wave 2");
                currentWave++;
                for (int i = currentWave * circleCount; i < circleCount + (circleCount * currentWave); i++)
                {
                    gc.GameStages.Peek().ToAdd.Add(circle[i]);
                    circle[i].IsActive = true;
                }
                Debug.WriteLine("ending circle wave 2");
            }

            Debug.WriteLine("pre killset gc");
            KillSet(gc);
            Debug.WriteLine("post killset gc");

            foreach (Circles ci in circle)
                if (ci.IsActive)
                {
                    ci.PathRadius++;
                    ci.Damage = (int)((float)(Core.Rnd(minDamage) + minDamage) * c.StatModifier.DamageMultiplier);
                }
            
            if (circle[circle.Count-1].PathRadius > maxRange && circle[circle.Count-1].IsActive)
                Reset(gc);

            Debug.WriteLine("ending action");
        }

        public override void Reset(GameController gc)
        {
            int nextSet = 0;
            float degrees = 0;
            currentWave = 0;
            for (int i = 0; i < circle.Count; i++)
            {
                circle[i].Health = 50000;
                if (i == nextSet)
                {
                    circle[i].Health = 1000;
                    degrees = 0;
                    nextSet += circleCount;

                }
                circle[i].IsActive = false;
                circle[i].Degrees = degrees;
                circle[i].PathRadius = pathRadius;
                if (gc.GameStages.Peek().CharList.Contains(circle[i]))
                    gc.GameStages.Peek().ToDelete.Add(circle[i]);
                degrees += degreesInc;
            }
            IsActive = false;
        }

        private void KillSet(GameController gc)
        {
            for (int i = 0; i < setCount; i++)
                if (circle[i * circleCount].Health <= 0 && circle[i * circleCount].IsActive)
                    for (int j = 0; j < circleCount; j++)
                        gc.GameStages.Peek().ToDelete.Add(circle[i * circleCount + j]);
        }

        private void CreateCircles(int centerX, int centerY)
        {
            float degrees = 0;
            for (int i = 0; i < circleCount; i++)
            {
                int health = 50000;
                Sprite circleSprite = Sprites.CreateSprite(Resources.LoadBitmap("touhou2.png"), 5, 6, 200, 200);
                if (i == 0)
                {
                    health = 1000;
                    circleSprite.CurrentCell = 1;
                }
                Circles tempCircle = new Circles("Touhou2",circleSprite,health, pathRadius, degrees, 0.07f*direction, centerX, centerY,false, false,c,isPlayer);
                circle.Add(tempCircle);
                degrees += degreesInc;
            }
            direction *= -1;
        }
    }
}
